#include "ColorPlane.h"
#include <iostream>
#include "digital_process.h"
#include <QRgb>

struct ComponentRange{
	int min;
	int max;
};

struct ComponentRange g_quan_range[COMPONENT_COUNT] = {
	{16, 235},
	{16, 240},
	{16, 240},		
};


void ColorPlane::ChooseValueChanged(int value)
{
	choose_value = value;
	draw_color_plane();
} 

int ColorPlane::draw_color_plane()
{
	//以下是widget中实际用于绘图的矩阵，原点位于左下角
	//int width = image->width();
	//int height = image->height();
	//int origin_x = 0;
	//int origin_y = height-1;
	int i = 0;
	int j = 0;
	int com1;
	int com2;
	
	int min = 16;
	int max = 240;
	for (i = min; i < max; ++i) {
		for(j = min; j < max; ++j) {
			//com1 = i * g_range[x_coor].max / width;
			//com2 = j * g_range[y_coor].max / height;
			com1 = i;
			com2 = j;
			int Y, U, V;
			if (x_coor == COMPONENT_U && y_coor == COMPONENT_V) {
				Y = choose_value;
				U = com1;
				V = com2;
			}else {
				std::cout << "not implement" <<std::endl;
				return -1;
			}
			unsigned char R, G, B;
			get_rgb_by_yuv_pixel(Y, U, V, &R, &G, &B);
			image->setPixel(i, max-j, qRgb(R, G, B));
			/*
			if( (i == min && j == min) || (i == min && j == max) || (i == max &&j == max/2)) {
				QString str = QString("%1 %2: %3 %4 %5 -> %6 %7 %8").arg(i).arg(j).arg(Y).arg(U).arg(V).arg(R).arg(G).arg(B);
				std::cout << str.toStdString().data() <<std::endl;
			}
			*/
		}
	}
	label->setPixmap(QPixmap::fromImage(*image));
	return 0;
}

ColorPlane::ColorPlane()
{
	this->resize(512, 512);
	x_coor = COMPONENT_U;
	y_coor = COMPONENT_V;
	image = new QImage(this->width(), this->height(), QImage::Format_RGB888);
	label = new QLabel();
	QString str("Y");
	scroll_area = new ScrollArea(str);
	layout = new QGridLayout(this);
	layout->addWidget(label, 0, 0, 1, 1);
	layout->addWidget(scroll_area, 0, 1, 1, 1);
	scroll_area->getSlider()->setRange(16, 235);
	connect(scroll_area->getSlider(), SIGNAL(valueChanged(int)), this, SLOT(ChooseValueChanged(int)));
	draw_color_plane();
	
}


